//
//  CAEmitterViewController.m
//  ZBAnimationDemo
//
//  Created by apple on 2018/7/10.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "CAEmitterViewController.h"

@interface CAEmitterViewController ()

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) UIButton *addButton;

@property (nonatomic, strong) UIButton *reduceButton;

@property (nonatomic, strong) UIButton *emojoButton;

//下雨效果
@property (nonatomic, strong) CAEmitterLayer * rainLayer;

//点赞红星
@property (nonatomic, strong) CAEmitterLayer * praiseLayer;


//笑
@property (nonatomic, strong) CAEmitterLayer * smeilLayer;

@end

@implementation CAEmitterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"粒子效果";
    [self.view addSubview:self.backImageView];
    [self.view addSubview:self.addButton];
    [self.view addSubview:self.reduceButton];
    [self.view addSubview:self.emojoButton];
    [self.backImageView.layer addSublayer:self.rainLayer];
    [self.backImageView.layer addSublayer:self.praiseLayer];
    [self.backImageView.layer addSublayer:self.smeilLayer];
    
}


#pragma mark - 表情
- (void)emojoButtonClick:(UIButton *)button
{
    BOOL select = button.isSelected;
    if(select){
        [self.smeilLayer setValue:@(1) forKey:@"birthRate"];
    }else{
        [self.smeilLayer setValue:@(0) forKey:@"birthRate"];
    }
    button.selected = !button.isSelected;
}


#pragma mark - 雨
- (void)addButtonClick
{
    NSInteger rate = 1;
    CGFloat scale = 0.05;
    if (self.rainLayer.birthRate < 30) {
        [self.rainLayer setValue:@(self.rainLayer.birthRate + rate) forKeyPath:@"birthRate"];
        [self.rainLayer setValue:@(self.rainLayer.scale + scale) forKeyPath:@"scale"];
    }
}

- (void)reduceButtonClick
{
    NSInteger rate = 1;
    CGFloat scale = 0.05;
    if (self.rainLayer.birthRate > 1) {
        [self.rainLayer setValue:@(self.rainLayer.birthRate - rate) forKeyPath:@"birthRate"];
        [self.rainLayer setValue:@(self.rainLayer.scale - scale) forKeyPath:@"scale"];
    }
}



#pragma mark - 点击桃心
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    CGPoint point = [self locationFromTouchs:touches];
    [self moveClickInPsition:point];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [self locationFromTouchs:touches];
    [self moveClickInPsition:point];
}


/**
 * 获取手指所在点
 */
- (CGPoint)locationFromTouchs:(NSSet<UITouch *> *)touches{
    UITouch * touch = [touches anyObject];
    return [touch locationInView:self.view];
}

/**
 * 移动发射源到某个点上
 */
- (void)moveClickInPsition:(CGPoint)position{
    
    CABasicAnimation * anim = [CABasicAnimation animationWithKeyPath:@"emitterCells.snowCell.scale"];
    anim.fromValue = @0.1f;
    anim.toValue = @0.5f;
    anim.duration = 0.5f;
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    // 用事务包装隐式动画
    [CATransaction begin];
    [CATransaction setDisableActions:YES];
    [self.praiseLayer addAnimation:anim forKey:nil];
    [self.praiseLayer setValue:[NSValue valueWithCGPoint:position] forKeyPath:@"emitterPosition"];
    [CATransaction commit];
}


#pragma mark - lazy
- (UIImageView *)backImageView
{
    if(_backImageView == nil)
    {
        _backImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _backImageView.contentMode = UIViewContentModeScaleAspectFill;
        _backImageView.clipsToBounds = YES;
        _backImageView.image = [UIImage imageNamed:@"Snip20180710_41"];
    }
    return _backImageView;
}

- (UIButton *)addButton
{
    if(_addButton == nil)
    {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _addButton.frame = CGRectMake(20, self.view.bounds.size.height - 60, 80, 40);
        _addButton.backgroundColor = [UIColor whiteColor];
        [_addButton setTitle:@"大点雨" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [_addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (UIButton *)reduceButton
{
    if(_reduceButton == nil)
    {
        _reduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _reduceButton.frame = CGRectMake(self.view.bounds.size.width - 100, self.view.bounds.size.height - 60, 80, 40);
        _reduceButton.backgroundColor = [UIColor whiteColor];
        [_reduceButton setTitle:@"小点雨" forState:UIControlStateNormal];
        [_reduceButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_reduceButton setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [_reduceButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reduceButton;
}
- (UIButton *)emojoButton
{
    if(_emojoButton == nil)
    {
        _emojoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _emojoButton.frame = CGRectMake(120, self.view.bounds.size.height - 60, 80, 40);
        _emojoButton.backgroundColor = [UIColor whiteColor];
        [_emojoButton setTitle:@"来点表情" forState:UIControlStateNormal];
        [_emojoButton setTitle:@"不要啦" forState:UIControlStateSelected];
        [_emojoButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_emojoButton addTarget:self action:@selector(emojoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _emojoButton;
}

- (CAEmitterLayer *)rainLayer
{
    if(_rainLayer == nil)
    {
        _rainLayer = [CAEmitterLayer layer];
        //1.发射器的形状
        _rainLayer.emitterShape = kCAEmitterLayerLine;
        //2.发射器发射模式
        _rainLayer.emitterMode = kCAEmitterLayerOutline;
        //3.发射器渲染模式
        _rainLayer.renderMode = kCAEmitterLayerOldestLast;
        //4.发射器发射器的尺寸
        _rainLayer.emitterSize = CGSizeMake(self.view.bounds.size.width , 5);
        
        _rainLayer.birthRate = 5;
        
        //5.发射器在xy平面的中心位置
        _rainLayer.emitterPosition = CGPointMake(self.view.bounds.size.width * 0.5, -10);
        
        // 2. 配置cell
        CAEmitterCell * snowCell = [CAEmitterCell emitterCell];
        snowCell.contents = (id)[[UIImage imageNamed:@"rain_white"] CGImage];
        
        //粒子产生速度，默认1个每秒
        snowCell.birthRate = 10;
        //粒子纯在时间，默认1秒
        snowCell.lifetime = 20.f;
        //速度
        snowCell.speed = 10.f;
        
        snowCell.velocity = 10.f;
        snowCell.velocityRange = 10.f;
        snowCell.yAcceleration = 1000.f;
        
        snowCell.scale = 0.1;
        snowCell.scaleRange = 0.f;
        
        // 3.添加到图层上
        _rainLayer.emitterCells = @[snowCell];
        
        
    }
    return _rainLayer;
}
- (CAEmitterLayer *)praiseLayer
{
    if(_praiseLayer == nil)
    {
        _praiseLayer = [CAEmitterLayer layer];
        //1.发射器的形状
        _praiseLayer.emitterShape = kCAEmitterLayerPoint;
        //2.发射器发射模式
        _praiseLayer.emitterMode = kCAEmitterLayerPoints;
        //3.发射器渲染模式
        _praiseLayer.renderMode = kCAEmitterLayerOldestLast;
        //4.发射器发射器的尺寸
        _praiseLayer.emitterSize = CGSizeMake(10 , 10);
        
        // 2. 配置cell
        CAEmitterCell * snowCell = [CAEmitterCell emitterCell];
        snowCell.contents = (id)[[UIImage imageNamed:@"love"] CGImage];
        snowCell.name = @"snowCell";
        
        //粒子产生速度，默认1个每秒
        snowCell.birthRate = 20;
        //粒子纯在时间，默认1秒
        snowCell.lifetime = 5;
        
        
        //粒子运动速度
        snowCell.velocity = 30;
        //速度范围
        snowCell.velocityRange = 100;
        //粒子z方向的加速度分量
        snowCell.yAcceleration = -500.f;
        
        //缩放比例
        snowCell.scale = 0.5;
        //缩放比例范围
        snowCell.scaleRange = 0.5;
        snowCell.scaleSpeed = 0.2;
        
        snowCell.alphaRange = 0.5;
        snowCell.alphaSpeed = 0.5;
        
        // 3.添加到图层上
        _praiseLayer.emitterCells = @[snowCell];
    }
    return _praiseLayer;
}
- (CAEmitterLayer *)smeilLayer
{
    if(_smeilLayer == nil)
    {
        _smeilLayer = [CAEmitterLayer layer];
        //1.发射器的形状
        _smeilLayer.emitterShape = kCAEmitterLayerLine;
        //2.发射器发射模式
        _smeilLayer.emitterMode = kCAEmitterLayerSurface;
        //3.发射器渲染模式
        _smeilLayer.renderMode = kCAEmitterLayerOldestLast;
        //4.发射器发射器的尺寸
        _smeilLayer.emitterSize = CGSizeMake(self.view.bounds.size.width , 10);
        
        _smeilLayer.emitterPosition = CGPointMake(self.view.bounds.size.width * 0.5, -10);
        
        _smeilLayer.birthRate = 0;
        
        CAEmitterCell *smeilCell = [CAEmitterCell emitterCell];
        smeilCell.name = @"smeilCell";
        
        smeilCell.contents = (id)[[UIImage imageNamed:@"smile"] CGImage];  // 粒子的内容 是CGImageRef类型的
        
        smeilCell.birthRate = 40;  // 每秒产生的粒子数量
        smeilCell.lifetime = 20.f;  // 粒子的生命周期
        
        smeilCell.velocity = 10.f;  // 粒子的速度
        smeilCell.yAcceleration = 500.f; // 粒子再y方向的加速的
        
        smeilCell.scale = 0.5;  //粒子的缩放比例
        smeilCell.speed = 0.5;
        
        _smeilLayer.emitterCells = @[smeilCell];  // 粒子添加到CAEmitterLayer上
        
        
    }
    return _smeilLayer;
}




@end
